<template>
  <div class="img-box">
    <div v-for="item in imgs" class="img-item">
      <img
        @click="openPopup(item)"
        :src="urlFormat(item)"
        alt=""
        style="width: 100px; height: 100px"
      />
    </div>
  </div>

  <el-dialog v-model="dialogVisible" title="预览" width="30%" append-to-body>
    <img
      :src="
        urlFormat({
          url: dialogImageUrl,
        })
      "
      style="display: block; max-width: 100%; margin: 0 auto"
    />
    <div style="text-align: right; margin-top: 20px">
      <el-button type="primary" @click="downLoad(dialogImageUrl)"
        >下载</el-button
      >
    </div>
  </el-dialog>
</template>

<script setup>
import { defineProps, ref } from "vue";
const props = defineProps({
  imgs: {
    type: Array || String,
    required: () => {},
  },
});

const dialogVisible = ref(false);
const dialogImageUrl = ref("");
const openPopup = (item) => {
  console.log(item);
  if (item.fileUrl) {
    item.url = item.fileUrl;
  }
  dialogVisible.value = true;
  dialogImageUrl.value = item.url;
};
const urlFormat = (item) => {
  let url = item.url || item.fileUrl;
  if (url) {
    return `/dev-api/file/download?url=${url}`;
  }
};
function downLoad(url) {
  downLoadFile(url, Date.now() + ".png");
}
</script>

<style lang="scss" scoped>
.img-box {
  width: 100%;
  height: 100px;
  z-index: 999;
  display: flex;
}
</style>
